import axios from 'axios';
import { addSpin, removeSpin } from '../utils/Spin';

export default function request(option) {
  const { isSpin = true } = option;
  return new Promise((resolve, reject) => {
    const instance = axios.create({
      timeout:5000,
    });
    instance.interceptors.request.use(
      config => {
        isSpin && addSpin();
        return config;
      },
      err => {
        isSpin && removeSpin();
        return err;
      }
    );
    instance.interceptors.response.use(
      response => {
        isSpin && removeSpin();
        return response;
      },
      err => {
        isSpin && removeSpin();
        return err;
      }
    );
    instance(option)
      .then(res => {
        resolve(res);
      })
      .catch(err => {
        isSpin && removeSpin()
        reject(err);
      });
  });
}
